angular.module('Controller')
.controller 'ProductServiceCtrl', ($scope, $state, menuManage, commonUtil, userData, $localStorage, commonConstant, wfAlert, $http) ->
    $scope.services=[]
    $('html, body').animate({
        scrollTop: $("#header").offset().top
    }, 100);
    $scope.currentDetailIndex=0
    $scope.getDetail =(id) ->
        $http.get("/wains/#{id}")
        .success (resp)->
            if resp.code==0
                console.log(resp+'成功获取')
                $scope.info = resp.message
                $scope.currentId = id
                console.log "ProductServiceCtrl",$scope.info
                $scope.wainDetailVosList = resp.message.wainDetailVos
                if $scope.wainDetailVosList?.length>0
                    $scope.currentDetailIndex=0
                    $scope.currentDetail=$scope.wainDetailVosList[0]
                    _.defer(()->
                        $scope.mobileTabWidth()
                        tabCurItem=$('.mbcp-tab.current')
                        if tabCurItem
                            tabCurItem.css('left','0')
                    )
                console.log($scope.wainDetailVosList)
            else
                wfAlert.toastError(resp.message)
        .error (err)->
            wfAlert.toastError(err.message)
    $scope.mobileTabWidth=()->
        tabItems=$('.mbcp-tab')
        console.log(tabItems)
        width=0
        switch $scope.wainDetailVosList.length
            when 1 then width=100
            when 2 then width=50
            when 3 then width=33.3
            when 4 then width=25
            else width=0
        if width!=0
            tabItems.css('width',width+'%')
        $scope.tabWidth=width
    $scope.pickDetail=(item,index)->
        $scope.currentDetail=item
        $scope.currentDetailIndex=index
        console.log($scope.currentDetailIndex,$scope.currentDetail)
        tabCurItem=$('.mbcp-tab.current')
        if tabCurItem
            tabCurItem.css('left',index*$scope.tabWidth+'%')
        ''
    $scope.pcNavWidth=()->
        if $scope.services.length<=0
            ''
        else
            'width:'+(100/$scope.services.length).toFixed(2)+'%'
    _.extend $scope,
        services: if $localStorage['language'] == 0 then $localStorage['wainCN'] else $localStorage['wainEN']
        currentId:$state.params.childId
    console.log('services',$scope.services)
    $scope.$on 'language.changed', ->
        $scope.services = if $localStorage['language'] == 0 then $localStorage['wainCN'] else $localStorage['wainEN']
    dynamicNavWid=()->
        return if $scope.services.length<2
        wordWid=0
        _.each $('.nav-item-tag'),(tagElem)->
            wordWid+=tagElem.scrollWidth
        navWid=$('.public-pc-nav').parent().css('width').replace('px','')
        sumWid=parseInt(navWid)-32
        marginWid=((sumWid-wordWid)/($('.nav-item-tag').length-1)).toFixed(4)
        marginWid=8 if marginWid<8
        $(".nav-item-tag").css('margin-right',marginWid+'px')
    _.defer(()->
        dynamicNavWid()
    )
    $scope.getDetail($scope.currentId)


